﻿using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理
{
    public class DrawingImage
    {
        /// <summary>
        /// Drawing a UpImage
        /// </summary>
        /// <param name="size">Size of Image</param>
        /// <returns>returns a BitMap</returns>
        public static Bitmap UpImage(Size size)
        {
            Brush brush = System.Drawing.Brushes.BlueViolet;
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (Pen pen = new Pen(brush, 2))
                {
                    g.DrawLine(pen, size.Width / 5, size.Height * 3 / 5, size.Width / 2, size.Height * 1 / 5);
                    g.DrawLine(pen, size.Width / 2, size.Height * 1 / 5, size.Width * 4 / 5, size.Height * 3 / 5);
                }
            }

            return bitmap;
        }
        /// <summary>
        /// Drawing a DownImage
        /// </summary>
        /// <param name="size">Size of Image</param>
        /// <returns>returns a BitMap</returns>
        public static Bitmap DownImage(Size size)
        {
            Brush brush = System.Drawing.Brushes.BlueViolet;
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (Pen pen = new Pen(brush, 2))
                {
                    g.DrawLine(pen, size.Width / 5, size.Height / 5, size.Width / 2, size.Height * 3 / 5);
                    g.DrawLine(pen, size.Width / 2, size.Height * 3 / 5, size.Width * 4 / 5, size.Height * 1 / 5);
                }
            }

            return bitmap;
        }
        /// <summary>
        /// paint the Control to a circle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void RoundButton_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Control control = (Control)sender;
            int maxLenth = control.Height > control.Width ? control.Height : control.Width;
            control.Size = new Size(maxLenth, maxLenth);
            System.Drawing.Drawing2D.GraphicsPath buttonPath =
                new System.Drawing.Drawing2D.GraphicsPath();

            // Set a new rectangle to the same size as the button's 
            // ClientRectangle property.
            //获取按钮原本的外框大小
            Rectangle newRectangle = control.ClientRectangle;
            //绘出按钮边界
            e.Graphics.DrawEllipse(Pens.Black, newRectangle);
            newRectangle.Inflate(-4, -4);
            //在新的方框内画一个圆
            buttonPath.AddEllipse(newRectangle);

            // Set the button's Region property to the newly created 
            // circle region.
            control.Region = new Region(buttonPath);
        }
        /// <summary>
        /// renamed headerText of DataGridView
        /// </summary>
        /// <param name="dgv">the DataGridView</param>
        /// <param name="sqlConnection">sql connection</param>
        public static void ReNameDataGridView(DataGridView dgv, System.Data.SqlClient.SqlConnection sqlConnection)
        {
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                dgv.Columns[i].HeaderText = SqlClassLibrary.SqlGetSet.GetColumnChnName(dgv.Columns[i].Name, sqlConnection);
                if (dgv.Columns[i].Name == "custid" ||
                    dgv.Columns[i].Name == "hetongid" ||
                    dgv.Columns[i].Name == "pym" ||
                    dgv.Columns[i].Name == "custid" ||
                    dgv.Columns[i].Name == "planID" ||
                    dgv.Columns[i].Name == "userName"
                    )
                {
                    dgv.Columns[i].Visible = false;
                }
            }

        }
        /// <summary>
        /// set some DataGridViewRows backcolor every interval some rows
        /// </summary>
        /// <param name="dgv">the DataGridView</param>
        /// <param name="rowsInterval">Every interval row</param>
        public static void ColorLines(DataGridView dgv, int rowsInterval, Color[] color)
        {
            ;//Interval [rowsInterval] row set color

            List<int> lines = new List<int>();
            for (int i = 1; i <= dgv.RowCount; i++)
            {
                if (rowsInterval == 0)
                {

                }
                else
                if (rowsInterval == 1)
                {
                    if (i % 2 == 0)
                        lines.Add(i);
                }
                else
                {
                    if ((i % (rowsInterval * 2)) - rowsInterval > 0 || (i % (rowsInterval * 2)) == 0)
                        lines.Add(i);
                }
            }

            if (dgv.Rows.Count > rowsInterval)
                for (int j = rowsInterval; j < dgv.RowCount; j++)
                {
                    if (lines.Contains(j + 1))
                        dgv.Rows[j].DefaultCellStyle.BackColor = color[0];
                    else
                        dgv.Rows[j].DefaultCellStyle.BackColor = color[1];
                }
        }
        /// <summary>
        /// Set The Single Row DataTable to Object
        /// </summary>
        /// <typeparam name="T">insert and returns type</typeparam>
        /// <param name="dataTable">a single row datatable</param>
        /// <param name="obj">insert object</param>
        /// <returns>returns a type of insert object</returns>
        public static T DataTableToObject<T>(System.Data.DataTable dataTable, T obj)
        {
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                string ColumnsName = dataTable.Columns[i].ColumnName;
                foreach (PropertyInfo obj_property in obj.GetType().GetProperties())
                {
                    if (obj_property.Name.ToUpper() == ColumnsName.ToUpper())
                    {
                        ColumnsName = obj_property.Name;
                        continue;
                    }
                }
                if (obj.GetType().GetProperty(ColumnsName) == null)
                {
                    continue;
                }
                obj.GetType().GetProperty(ColumnsName).SetValue(obj, dataTable.Rows[0][i].ToString());
            }

            return obj;
        }
        /// <summary>
        /// Set The DataGridView's One Selected Row to Object
        /// </summary>
        /// <typeparam name="T">insert and returns type</typeparam>
        /// <param name="dataGridView">a single row dataGridView</param>
        /// <param name="obj">insert object</param>
        /// <returns>returns a type of insert object</returns>
        public static T DataGridViewSelectedRowToObject<T>(DataGridView dataGridView, T obj)
        {
            DataGridViewRow dgvr = dataGridView.CurrentCell.OwningRow;
            string ColumnName = "";
            string ColumnValue = "";
            for (int i = 0; i < dataGridView.Columns.Count; i++)
            {
                ColumnName = dataGridView.Columns[i].Name;
                ColumnValue = dgvr.Cells[i].Value.ToString();
                foreach (PropertyInfo item in obj.GetType().GetProperties())
                {
                    if (item.Name.ToUpper() == ColumnName.ToUpper())
                    {
                        ColumnName = item.Name;
                        continue;
                    }
                }
                if (obj.GetType().GetProperty(ColumnName) == null)
                {
                    continue;
                }
                obj.GetType().GetProperty(ColumnName).SetValue(obj, ColumnValue);
            }

            return obj;
        }
        /// <summary>
        /// Set The DataGridView To a DataTable
        /// </summary>
        /// <param name="dgv">insert DataGridView </param>
        /// <param name="columnType">the type of columns,use the dataGridView's headerText or Name</param>
        /// <param name="tableName">the dataTable's name</param>
        /// <returns>returns a dataTable</returns>
        public static DataTable DataGridViewToDataTable(DataGridView dgv, ColumnType columnType, string tableName, bool IsSelectedRows)
        {
            DataTable dataTable = new DataTable(tableName);
            int[] indexes = new int[dgv.ColumnCount];
            int j = 0;
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].Visible)
                {
                    dataTable.Columns.Add(columnType == ColumnType.HeaderText ? dgv.Columns[i].HeaderText : dgv.Columns[i].Name);
                    indexes[j] = i;
                    j++;
                }
            }
            if (IsSelectedRows && dgv.SelectedRows.Count > 1)
                for (int i = 0; i < dgv.SelectedRows.Count; i++)
                {
                    if (dgv.SelectedRows[i].Visible)
                    {
                        DataRow dataRow = dataTable.NewRow();
                        for (int K = 0; K < dataTable.Columns.Count; K++)
                        {
                            dataRow[K] = dgv.SelectedRows[i].Cells[indexes[K]].Value;
                        }
                        dataTable.Rows.Add(dataRow);
                    }
                }
            else
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    if (dgv.Rows[i].Visible)
                    {
                        DataRow dataRow = dataTable.NewRow();
                        for (int K = 0; K < dataTable.Columns.Count; K++)
                        {
                            dataRow[K] = dgv.Rows[i].Cells[indexes[K]].Value;
                        }
                        dataTable.Rows.Add(dataRow);
                    }
                }

            return dataTable;
        }
        public static List<T> DataColumnToList<T>(System.Data.DataColumn dataColumn, List<T> obj)
        {
            T t = default(T);
            for (int i = 0; i < dataColumn.Table.Rows.Count; i++)
            {
                if (dataColumn.Table.Rows[i][0] != null)
                {
                    t.GetType().GetProperty(dataColumn.ColumnName).SetValue(t, dataColumn.Table.Rows[i][0]);
                    obj.Add(t);
                }
            }
            return obj;
        }
    }
}
